define(['app'], function(app) {
    app.controller('CopyMockController', function($http, xContext, $uibModalInstance, toastr) {
        var self = this;
        self.type = '';
        self.source = '';
        self.target = '';
        self.ignore = {
            folder: true,
            file: true
        };

        self.init = function() {
            self.type = xContext.type;
            var nodeName = xContext.node.name;
            if (self.type == 'file') {
                nodeName = nodeName.substring(0, nodeName.lastIndexOf('.'));
            }
            self.source = xContext.node.parent + '/' + nodeName;
        }

        self.copy = function(valid) {
            if (valid && self.target != self.source) {
                $http.post('/mock/data/copy', {
                    type: self.type,
                    module: xContext.module,
                    source: self.source + (self.type == 'file' ? '.json' : ''),
                    target: self.target + (self.type == 'file' ? '.json' : ''),
                    ignore: self.ignore
                }).then(function(response) {
                    if (response) {
                        if (self.type == 'file') {
                            toastr.success('文件已复制！');
                        } else {
                            toastr.success('文件夹已复制！');
                        }
                        $uibModalInstance.close({
                            path: self.target + (self.type == 'file' ? '.json' : '/')
                        });
                    }
                });
            }
        }

        self.cancel = function() {
            $uibModalInstance.dismiss();
        }
    });
});
